package mapEditor.view.mapEdit
{
	import asset.mapEditorScenePropertyPanelViewAsset;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mapEditor.MapEditorManager;
	import mapEditor.data.ConfigTemplateInfo;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.view.PropertyItemView;
	import mapEditor.vo.MapTerrainTileVO;
	
	public class TerrainPropertyView extends mapEditorScenePropertyPanelViewAsset
	{
		private var _itemList:Vector.<PropertyItemView>=new Vector.<PropertyItemView>();
		/**
		 *属性面板类型:1=地形属性;2=地块属性 
		 */		
		private var _type:int=1;
		private var _mapTerrainTileVO:MapTerrainTileVO;
		
		public function TerrainPropertyView()
		{
			setView();
			addEvent();
		}
		
		private function setView():void
		{
			update();
		}
		
		private function addEvent():void
		{
			pnlSet.chkVisibleGrid.addEventListener(MouseEvent.CLICK, __onChkClick);
			pnlSet.chkVisibleAsset.addEventListener(MouseEvent.CLICK, __onChkClick);
			MapEditorManager.instance.addEventListener(MapEditorEvent.MAP_TERRAIN_TILE_SELECTED, __onMapTerrainTileSelected);
		}
		
		private function removeEvent():void
		{
			pnlSet.chkVisibleGrid.removeEventListener(MouseEvent.CLICK, __onChkClick);
			pnlSet.chkVisibleAsset.removeEventListener(MouseEvent.CLICK, __onChkClick);
			MapEditorManager.instance.removeEventListener(MapEditorEvent.MAP_TERRAIN_TILE_SELECTED, __onMapTerrainTileSelected);
		}
		
		private function __onMapTerrainTileSelected(evt:MapEditorEvent):void
		{
			if(_mapTerrainTileVO)_mapTerrainTileVO.removeEventListener(Event.CHANGE, __onTerrainTileVOPropertyChange);
			_mapTerrainTileVO=evt.date as MapTerrainTileVO ;
			if(_mapTerrainTileVO)
			{
				_mapTerrainTileVO.addEventListener(Event.CHANGE, __onTerrainTileVOPropertyChange);
				_type=2;
			}
			else
			{
				_type=1;
			}
			update();
		}
		
		private function __onTerrainTileVOPropertyChange(evt:Event):void
		{
			update();
		}
		
		private function __onChkClick(evt:MouseEvent):void
		{
			switch(evt.currentTarget)
			{
				case pnlSet.chkVisibleGrid://显示网络
					MapEditorManager.instance.mapEditorModel.editAreaVO.visibleGrid=pnlSet.chkVisibleGrid.selected;
					MapEditorManager.instance.mapEditorModel.editAreaVO.change();
					break;
				case pnlSet.chkVisibleAsset:
					break;
			}
		}
		
		private function setTerrain():void
		{
			clearAllItem();
			
			var propertyItemView:PropertyItemView=new PropertyItemView("行大小", ConfigTemplateInfo.instance.mapVO.mapRow.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("列大小", ConfigTemplateInfo.instance.mapVO.mapCols.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			propertyItemView.gotoAndStop(2);
			addItem(propertyItemView);
			
			propertyItemView=new PropertyItemView("块大小",ConfigTemplateInfo.instance.mapVO.tileSize.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			
			posPropertyList.graphics.clear();
			posPropertyList.graphics.lineStyle(1, 0x6e6e6e);
			posPropertyList.graphics.moveTo(0, 0);
			posPropertyList.graphics.lineTo(0, posPropertyList.height);
			posPropertyList.graphics.lineTo(174, posPropertyList.height-1);
			posPropertyList.graphics.lineTo(174, 0);
			posPropertyList.graphics.endFill();
		}
		
		private function setTerrainTile():void
		{
			clearAllItem();
			
			var propertyItemView:PropertyItemView=new PropertyItemView("所在行",_mapTerrainTileVO.y.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("所在列", _mapTerrainTileVO.x.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			propertyItemView.gotoAndStop(2);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("块高",_mapTerrainTileVO.height.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("底高",_mapTerrainTileVO.bottom.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			propertyItemView.gotoAndStop(2);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("通行",_mapTerrainTileVO.walkable.toString(), 2);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			
			posPropertyList.graphics.clear();
			posPropertyList.graphics.lineStyle(1, 0x6e6e6e);
			posPropertyList.graphics.moveTo(0, 0);
			posPropertyList.graphics.lineTo(0, posPropertyList.height);
			posPropertyList.graphics.lineTo(174, posPropertyList.height-1);
			posPropertyList.graphics.lineTo(174, 0);
			posPropertyList.graphics.endFill();
		}
		
		private function addItem(item:PropertyItemView):void
		{
			item.x=1;
			item.y=_itemList.length*item.height;
			posPropertyList.addChild(item);
			_itemList.push(item);
		}
		
		private function update():void
		{
			tabTerrain.gotoAndStop(2);
			tabGrid.gotoAndStop(2);
			switch(_type)
			{
				case 1:
					tabTerrain.gotoAndStop(1);
					setTerrain();
					break;
				case 2:
					tabGrid.gotoAndStop(1);
					setTerrainTile();
					break;
			}
			pnlSet.y=posPropertyList.y+posPropertyList.height+5;
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		private function clearAllItem():void
		{
			while(_itemList && _itemList.length>0)
			{
				var propertyItemView:PropertyItemView=_itemList.shift();
				propertyItemView.removeEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
				propertyItemView.dispose();
				propertyItemView=null;
			}
		}
		
		private function __onPropertyItemChange(evt:MapEditorEvent):void
		{
			var propertyItemView:PropertyItemView=evt.currentTarget as PropertyItemView;
			switch(_type)
			{
				case 1:
					switch(propertyItemView.propertyName)
					{
						case "行大小":
							ConfigTemplateInfo.instance.mapVO.mapRow=int(propertyItemView.value);
							break;
						case "列大小":
							ConfigTemplateInfo.instance.mapVO.mapCols=int(propertyItemView.value);
							break;
						case "块大小":
							ConfigTemplateInfo.instance.mapVO.tileSize=int(propertyItemView.value);
							break;
					}
					ConfigTemplateInfo.instance.mapVO.change();
					break;
				case 2:
					switch(propertyItemView.propertyName)
					{
						case "所在行":
							_mapTerrainTileVO.y=int(propertyItemView.value);
							break;
						case "所在列":
							_mapTerrainTileVO.x=int(propertyItemView.value);
							break;
						case "块高":
							_mapTerrainTileVO.height=int(propertyItemView.value);
							break;
						case "底高":
							_mapTerrainTileVO.bottom=int(propertyItemView.value);
							break;
						case "通行":
							_mapTerrainTileVO.walkable=propertyItemView.value=="true";
							break;
					}
					_mapTerrainTileVO.change();
					break;
			}
		}
		
		public function dispose():void
		{
			removeEvent();
			if(parent)parent.removeChild(this);
		}
	}
}